//
//  XSVideoListViewController.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/29.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSVideoListViewController.h"
#import "XSVideoListTableViewCell.h"

@interface XSVideoListViewController ()

@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UIButton *playView;

@end

@implementation XSVideoListViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];


}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self tapPlayView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    
    if (self.localPath.length == 0) {
        return;
    }
    
    [self loadLocalData];

}

- (void)setupUI{
    [self.tableView registerNib:[UINib nibWithNibName:@"XSVideoListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cellID"];

    UIBarButtonItem *deleteItem = [UIBarButtonItem backItemWithimage:nil highImage:nil target:self action:@selector(deleteAllMovie) title:@"删除"];
    self.navigationItem.rightBarButtonItem = deleteItem;
    
}
- (void)loadLocalData{
    NSArray *subPaths = [[NSFileManager defaultManager]subpathsAtPath:self.localPath];
    NSLog(@"%@",subPaths);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (NSString *str in subPaths) {
            if ([str hasSuffix:@"mov"] || [str hasSuffix:@"mp4"] || [str hasSuffix:@"MOV"]) {
                NSString *path = [self.localPath stringByAppendingPathComponent:str];
                UIImage *thumb = [XSAVTools getThumbnailImageFromFilePath:path time:1 isLocal:YES];
                NSDictionary *dic = @{@"path":path,@"image":thumb};
                [self.dataArray addObject:dic];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    });
    
}

- (void)deleteAllMovie{
//    for (NSDictionary *dic  in self.dataArray) {
//        [XSFileManager deletePath:dic[@"path"]];
//    }
    
    [XSFileManager clearTemp];

    [self.dataArray removeAllObjects];
    [self.tableView reloadData];
    
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _dataArray;
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XSVideoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
 
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.imageV.image = dic[@"image"];
    cell.titleLab.text = [dic[@"path"] lastPathComponent];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.dataArray[indexPath.row];
    [[XSAVPlayer shareInstance]playVideoWithURLString:dic[@"path"] onPlayView:self.playView isLocalFile:YES];
    
}
- (UIButton *)playView{
    if (!_playView) {
        _playView = [[UIButton alloc]initWithFrame:self.view.bounds];
        _playView.backgroundColor = [UIColor blackColor];
        [_playView addTarget:self action:@selector(tapPlayView) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_playView];
    }
    return _playView;
}
- (void)tapPlayView{
    [[XSAVPlayer shareInstance]sf_deallocPlayer];
    [_playView removeFromSuperview];
    _playView = nil;
    
}


@end
